package org.glickr.api.photos.geo

import org.glickr.api.FlickrApiMethod
import org.glickr.FlickrPhoto
import groovy.util.slurpersupport.GPathResult
import org.glickr.FlickrServicePluginException
import org.glickr.FlickrServiceApiException
import org.glickr.Location

/* ----------------------------------------------------------------------------------------------------
REQUEST: flickr.photos.geo.getLocation
INFO: Get the geo data (latitude and longitude and the accuracy level) for a photo.
AUTHENTICATION: This method does not require authentication.
ARGUMENTS:
api_key (Required): Your API application key.
photo_id (Required): The id of the photo you want to retrieve location data for.
extras (Optional): Extra flags.
EXAMPLE RESPONSE:
<photo id="123">
  <location latitude="-17.685895" longitude="-63.36914" accuracy="6" />
</photo>
---------------------------------------------------------------------------------------------------- */

class geoGetLocation implements FlickrApiMethod {
    //
    //  API METHOD
    //
    static final String apiMethod = 'flickr.photos.geo.getLocation'

    //
    //  VALIDATOR
    //
    Closure validatorClosure = { FlickrPhoto photo ->
        if (!photo || !photo?.id) { return [validated:false, message:"Required value 'FlickrPhoto.id' is missing"] }
        return [validated:true]
    }

    //
    //  PARAMS
    //
    Closure paramsClosure = { FlickrPhoto photo ->
        [photo_id:photo?.id, secret:(photo?.secret ?:'')]
    }

    //
    //  PROCESSOR
    //
    Closure processorClosure = { GPathResult response, FlickrPhoto photo ->
        photo.location = new Location()
        photo.location.latitude = response.photo.location.@latitude.toBigDecimal()
        photo.location.longitude = response.photo.location.@longitude.toBigDecimal()
        photo.location.accuracy = response.photo.location.@accuracy.toInteger()

        // todo locality / county / region / country
        // todo figure out the structure

        return photo
    }

//<rsp stat="ok">
//  <photo id="7957234984">
//    <location latitude="52.033115" longitude="5.057229" accuracy="14" context="0" place_id="FFzfBi9WUbvfl5o" woeid="731983">
//      <locality place_id="FFzfBi9WUbvfl5o" woeid="731983">Nieuwegein</locality>
//      <county place_id="r079msBQUL9l_i8msg" woeid="12592311">Nieuwegein</county>
//      <region place_id="5x2p3bBTUb4SksS8" woeid="2346381">Utrecht</region>
//      <country place_id="Exbw8apTUb6236fOVA" woeid="23424909">Netherlands</country>
//    </location>
//  </photo>
//</rsp>

    //
    //  ERROR CODES
    //
    Closure errorsClosure = { GPathResult response ->
        if (response.err.@code.toInteger() == 1) { return null }      // photo not found
        if (response.err.@code.toInteger() == 2) { return null }      // Photo has no location information

        // recoverable errors
        if ([100,105,116].contains(response.err.@code.toInteger())) {
            return new FlickrServiceApiException(response)
        } else {
            return new FlickrServicePluginException(response)
        }
    }
}